//
//  JGRequestHelper.m
//  JGRider
//
//  Created by 单启志 on 2018/3/8.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGRequestHelper.h"
#import "FQChainRequest.h"
#import "FQBaseRequest.h"
@interface JGRequestHelper()<YTKChainRequestDelegate>
@property(nonatomic,strong)MBProgressHUD *hud;
@property(nonatomic,copy)void(^invokeSuccessBlock)(id);
@property(nonatomic,copy)void(^invokeFaileBlock)(id);

@property(nonatomic,strong)FQBaseRequest *request;
@end
@implementation JGRequestHelper

+(instancetype)share{
    static JGRequestHelper *helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper=[[JGRequestHelper alloc]init];
    });
    return helper;
}

-(void)requestWithParams:(NSDictionary*)params requestApi:(Class)className hudMessage:(NSString*)hud view:(UIView*)view successBlock:(void(^)(id result))invokeSuccessBlock faileBlock:(void(^)(id error))invokeFaileBlock{
    _hud=[MBProgressHUD showMessage:hud ToView:view];
    _hud.mode=MBProgressHUDModeIndeterminate;
    FQGetSignApi *signReq=[[FQGetSignApi alloc]initWithParams:params];
    FQChainRequest *chainReq=[[FQChainRequest alloc]init];
    [chainReq addRequest:signReq callback:^(YTKChainRequest * _Nonnull chainRequest, YTKBaseRequest * _Nonnull baseRequest) {
        FQGetSignApi *req=(FQGetSignApi*)baseRequest;

        FQBaseRequest *api=[(FQBaseRequest*)[className alloc]initWithSi:[req si] Pa:[req pa]];
        [chainRequest addRequest:api callback:nil];

    }];
    chainReq.delegate=self;
    [chainReq start];

    self.invokeSuccessBlock=invokeSuccessBlock;
    self.invokeFaileBlock=invokeFaileBlock;

}

-(void)requestWithParams:(NSDictionary*)params requestApi:(Class)className view:(UIView*)view successBlock:(void(^)(id result))invokeSuccessBlock faileBlock:(void(^)(id error))invokeFaileBlock{
    return [self requestWithParams:params requestApi:className hudMessage:@"加载中..." view:view successBlock:invokeSuccessBlock faileBlock:invokeFaileBlock];
}


-(FQChainRequest *)requestWithParams:(NSDictionary*)params requestApi:(Class)className successBlock:(void(^)(id result))invokeSuccessBlock faileBlock:(void(^)(id error))invokeFaileBlock{

    FQGetSignApi *signReq=[[FQGetSignApi alloc]initWithParams:params];
    FQChainRequest *chainReq=[[FQChainRequest alloc]init];
    [chainReq addRequest:signReq callback:^(YTKChainRequest * _Nonnull chainRequest, YTKBaseRequest * _Nonnull baseRequest) {
        FQGetSignApi *req=(FQGetSignApi*)baseRequest;

        FQBaseRequest *api=[(FQBaseRequest*)[className alloc]initWithSi:[req si] Pa:[req pa]];
        [chainRequest addRequest:api callback:nil];

    }];
    chainReq.delegate=self;
    [chainReq start];

    self.invokeSuccessBlock=invokeSuccessBlock;
    self.invokeFaileBlock=invokeFaileBlock;

    return chainReq;
}

#pragma mark api delegate
-(void)chainRequestFinished:(YTKChainRequest *)chainRequest{
    NSDictionary *dic= chainRequest.requestArray[1].responseJSONObject;

    NSString *message=dic[@"data"][@"msg"];
    _hud.label.text=message;
    _hud.mode=MBProgressHUDModeText;
    [_hud hideAnimated:YES afterDelay:0.5];

    if (self.invokeSuccessBlock) {
        self.invokeSuccessBlock(dic);
    }
}

-(void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest *)request{
   NSString*msg=request.responseJSONObject[@"data"][@"msg"];
    _hud.label.text=FQIsEmpty(msg)?@"失败":msg;
    _hud.mode=MBProgressHUDModeText;
    [_hud hideAnimated:YES afterDelay:0.5];
    if (self.invokeFaileBlock) {
    self.invokeFaileBlock(request.responseJSONObject);
    }
}
@end
